﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LoA.Shared.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace LoA.CameraEngine.Entities {
	internal sealed class SecondPersonDynamicCameraProcessor : CameraProcessorBase {
		public SecondPersonDynamicCameraProcessor(Game game) {
			this.FieldOfView = MathHelper.ToRadians(45.0f);
			this.NearPlaneDistance = 0.02f;
			this.FarPlaneDistance = 700.0f;

			this.objectPosition = Vector3.UnitY * 2;
			this.objectDirection = Vector3.UnitX;
			this.targetOffset = new Vector3(0.0f, 1.85f, 0.0f);

			this.UpdateViewMatrix( );
		}

		private Vector3 objectPosition;
		public Vector3 ObjectPosition { get { return this.objectPosition; } set { this.objectPosition = value; } }
		private Vector3 objectDirection;
		public Vector3 ObjectDirection { get { return this.objectDirection; } set { this.objectDirection = value; } }
		private Vector3 objectUp;
		public Vector3 ObjectUp { get { return this.objectUp; } set { this.objectUp = value; } }
		private Vector3 targetOffset;
		public Vector3 TargetOffset { get { return this.targetOffset; } set { this.targetOffset = value; } }

		private void UpdateViewMatrix( ) {
			this.ViewMatrix = Matrix.CreateLookAt(
				this.ObjectPosition - this.ObjectDirection + this.TargetOffset,
				this.ObjectPosition + this.TargetOffset,
				Vector3.Up
			);
		}

		//MouseState? lastMouseState = null;
		//MouseState originalMouseState;
		public override void Update(GameTime gameTime, IInput inputEngine) {
			//MouseState mouseState = Mouse.GetState( );
			//if (mouseState.MiddleButton == ButtonState.Pressed) {
			//	if (lastMouseState == null) {
			//		originalMouseState = mouseState;
			//		Mouse.SetPosition(this.GraphicsDevice.Viewport.Width / 2, this.GraphicsDevice.Viewport.Height / 2);
			//		MouseState mouseState2 = Mouse.GetState( );
			//		lastMouseState = mouseState2;
			//		this.Game.IsMouseVisible = false;
			//	} else {
			//		int x = lastMouseState.Value.X - mouseState.X;
			//		int y = lastMouseState.Value.Y - mouseState.Y;

			//		float xP = x * 1.0f / this.GraphicsDevice.Viewport.Width;
			//		float yP = y * 1.0f / this.GraphicsDevice.Viewport.Height;

			//		this.Turn(this.character, Rotation.TurnRight, MathHelper.Pi * xP);

			//		Mouse.SetPosition(this.GraphicsDevice.Viewport.Width / 2, this.GraphicsDevice.Viewport.Height / 2);
			//		MouseState mouseState2 = Mouse.GetState( );
			//		lastMouseState = mouseState2;
			//	}
			//} else {
			//	if (lastMouseState != null) {
			//		Mouse.SetPosition(originalMouseState.X, originalMouseState.Y);
			//		lastMouseState = null;
			//		this.Game.IsMouseVisible = true;
			//	}
			//}

			this.UpdateViewMatrix( );
		}
	}
}
